package com.sfx.concurrent.demo.concurrent.TestState;

import lombok.extern.slf4j.Slf4j;

/**
 * Created with IntelliJ IDEA.
 * Description: 用于测试线程都有哪些状态
 * User: sfx
 * Date: 2023-01-06
 * Time: 18:06
 */

@Slf4j(topic = "c.TestState")
public class TestState {

    public static void main(String[] args) {
        //NEW 状态 -> 没有调用start方法->只创建了线程对象没有启动该线程
        Thread t1 = new Thread(() -> {
            log.debug("running...");
        },"t1");
        //Runnable状态中的可运行状态
        Thread t2 = new Thread(() -> {
            while(true) {

            }
        },"t2");
        //TIMED_WAITING状态
        Thread t3 = new Thread(()->{
            synchronized (TestState.class) {
                try {
                    Thread.sleep(100000000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"t3");
        //WAITING状态
        Thread t4 = new Thread(()->{
            try {
                t2.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"t4");
        //TERMINATED->终止状态
        Thread t5 = new Thread(()->{
           log.debug("running....");
        },"t5");
        //BLOCKED 状态
        Thread t6 = new Thread(()->{//后获取到锁
           synchronized (TestState.class) {
               log.debug("running");
           }
        },"t6");
        t2.start();
        t3.start();
        t4.start();
        t5.start();
        t6.start();
        System.out.println("t1 : " + t1.getState());
        System.out.println("t2 : " + t2.getState());
        System.out.println("t3 : " + t3.getState());
        System.out.println("t4 : " + t4.getState());
        System.out.println("t5 : " + t5.getState());
        System.out.println("t6 : " + t6.getState());
    }
}
